import axios from "axios";
import type { AxiosRequestConfig } from "axios";
import { UserInfo } from "./type";
import { moheAPI } from "./api";
import { sleep, sleepRand } from "../utils/time";

export const defaultConfig: AxiosRequestConfig = {
  baseURL: "https://api.icitybox.cn/index.php/api",
  timeout: 3000,
  headers: {
    Host: "api.icitybox.cn",
    Connection: "keep-alive",
    "content-type": "application/x-www-form-urlencoded",
    channel: "mini",
    platform: "wap",
    language: "zh_CN",
    "Platform-Id": 1,
    sign: process.env.MOHE_SIGN,
    token: process.env.MOHE_TOKEN,
    "Accept-Encoding": "gzip,compress,br,deflate",
    "User-Agent":
      "Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.47(0x18002f2c) NetType/WIFI Language/zh_CN",
    Referer: "https://servicewechat.com/wx8434e31068c20849/606/page-frame.html",
  },
};

export const http = axios.create(defaultConfig);

export const getUserInfo = async (): Promise<UserInfo> => {
  await sleepRand(1000, 3000);
  const res = await http.get(moheAPI.userInfo);
  return res.data;
};

export const signUp = async (): Promise<UserInfo> => {
  await sleepRand(1000, 3000);
  let param = "ts=" + new Date().getTime();
  const res = await http.get(`${moheAPI.sign}?${param}`);
  return res.data;
};
